<?php
	function executeFunc(){
		$jsParam=$_POST;
		if ($jsParam['flag']=="executeFunc") {
			if (!empty($jsParam['name'])) {
				$funcName=$jsParam['name'];
				$funcParam=explode(",", $jsParam['param']);
				@$result=call_user_func_array($funcName,$funcParam);
				if (empty($result)) {
					$returnResult=array(
						"result"=>"done",
						"status"=>"success"
					);
				}else{
					$returnResult=array(
						"result"=>$result,
						"status"=>"success"
					);
				}

				echo json_encode($returnResult);
			}
			
		}
		
	}

	class dealRequest{
		private $r;//外部自定义的请求处理对象
		private $reqClassify;//请求的分类
		private $reqContent;//请求的操作
		private $reqParam="";//请求的参数
		private $token=array();
		private $tokenLifetime;//tokenA的使用寿命单位为秒
		private $sameRequestInterval;//单位为秒，从客户端接受请求的间隔是件的最小值

		public function __construct($class,$sqi=1,$tlt=1800){
			$this->sameRequestInterval=$sqi;
			$this->tokenLifetime=$tlt;
			$this->r=$class;
			$this->checkRequest();		
			$this->reqParam=$_POST['content']?explode(",",$_POST['content']):null;
			$this->getToken();
			$this->deal();
		}
		public function deal(){
			$method=$this->reqContent;
			
			
			if (@$this->r->checkPower($this->reqClassify,$this->reqContent,$this->token['preusername'])) {
				$html=$this->r->$method($this->reqParam);
				setcookie("tokenA",$this->createNewTokenA(),time()+$this->tokenLifetime);
				echo $html;
			}else{
				echo "nopower";
			}

		}



		private function checkRequest(){
			if (!isset($_POST['request']) || $_POST['request']=="") {
				echo "request_error!";
				exit;
			}
			$tmp=explode("_", $_POST['request']);
			if (array_key_exists($tmp[0], $this->r->allowList)) {
				if(in_array($tmp[1], $this->r->allowList[$tmp[0]])){
					$this->reqClassify=$tmp[0];
					$this->reqContent=$tmp[1];
					return true;
				}else{
					echo "request_error!";
					exit;
				}
			}else{
				echo "request_error!";
				exit;
			}
		}
		private function getToken(){
			if (isset($_COOKIE['tokenA'])) {//判断是否存在tokenA
				$tmp1=$_COOKIE['tokenA'];
				$tmp1=base64_decode($tmp1);
				$tmp1_token=explode(";", $tmp1);
				$this->token['lasttime']=$tmp1_token[0];
				$this->token['lastusername']=$tmp1_token[1];
				$this->token['lastaction']=$tmp1_token[2];
				$tmp2=$_POST['tokenB'];
				$tmp2_token=explode(";", $tmp2);
				$this->token['pretime']=$tmp2_token[0];
				$this->token['preusername']=$tmp2_token[1];
				$this->token['preaction']=$tmp2_token[2];

			}else{
				echo "you don't have token!";
				exit();
			}
			$username=$_COOKIE['username'];
			if (empty($username)) {
				echo "client dont't set username";
				exit();
			}
			if (!($this->token["lastusername"]==$username&&$this->token["preusername"]==$username&&$this->token["lastusername"]==$this->token["preusername"])) {
				echo "your username can't use";
				exit();
			}
			if ($this->token['preaction']==$this->token['lastaction']) {
				$interval=$this->token['pretime']-$this->token['lasttime'];
				if (($interval-$interval%1000) < $this->sameRequestInterval) {
					echo "please send request slowly!";
					exit();
				}
			}
			
			
		}
		private function createNewTokenA(){
			$tokenA=$this->token['pretime'].";".$this->token['preusername'].";".$this->token['preaction'];
			return base64_encode($tokenA);
		}
	}

	
?>